
/*
*	jQuery verticalSlideOutMenu plugin
*	
*	Copyright (c) <2011> Anubhav Bhojne <anubhav.bhojne@gmail.com>
*
*	Permission is hereby granted, free of charge, to any person obtaining a copy
*	of this software and associated documentation files (the "Software"), to deal
*	in the Software without restriction, including without limitation the rights
*	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the Software is
*	furnished to do so, subject to the following conditions:

*	The above copyright notice and this permission notice shall be included in
*	all copies or substantial portions of the Software.

*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*	THE SOFTWARE.
*	
*/

(function($) {
$.fn.verticalSlideOutMenu = function(options) {
		
		var defaults = {
				accordion : false,
				active: 0,
				slideAnimation : 'easeOutBounce',
				positionFixed:false,
				positioning:'absolute',
				slideInOnLoad:  true,
				width : 300,
				selected : function(){}
				
			};
			
		var options = $.extend(defaults, options);
		
		var self = this;
		
		var $scrollDiv,$h3,$verticalSlideOutMenu,$slide,$animationMethod;
		
		var methods = {
		
			init: function()
			{
				self.addClass("verticalMenu");
				self.css("width",options.width);
				self.wrap("<div class='scrollDiv'></div>");
				$scrollDiv = self.parent();
		
				self.children("li").each(function(index,item){
					$(item).contents().filter(function(index,item){
						if(this.nodeName =='A' && index==0)
						{
							var $obj = $(item).wrap("<h3></h3>");
						}
					});
				});
		
				$h3 = self.find("h3");
				
				if(options.accordion)
				{
					var $test = $h3.eq(options.active).children().addClass("title expanded").parent();
					$h3.not($test).next().css('display','none').prev().children().addClass("title collapsed");
				}
				else
				{
					$h3.children().addClass("title expanded");
				}	
				 
				
				$h3.next().addClass("menu_body");
				
				$(".menu_body li:has(> ul)").each(function(index,item){
					$(this).children().eq(0).wrap("<span class='hasmenu'></span>");
				});
				
				$(".hasmenu",self).prepend("<a class='submenu closed'>&nbsp;&nbsp;&nbsp;</a>");
				 
				$(".hasmenu",self).next().css('display','none');
				
				$scrollDiv.wrap("<div class='verticalSlideOutMenu'></div>");
					
				$verticalSlideOutMenu = $scrollDiv.parent();	
				
				$verticalSlideOutMenu.before('<div class="slide slideIn"></div>'); 
				
					
				
				$slide = $verticalSlideOutMenu.prev();
				
				if($.browser.msie)
				{
					$slide.css("left",parseInt($verticalSlideOutMenu.width(), 10) -9);
				}
				else
				{
					$slide.css("left",parseInt($verticalSlideOutMenu.width(), 10) -2);
				}	
				
				$slide.css("top",$verticalSlideOutMenu.height()/2 -37);
				
				$(".menu_body",self).find("a:not('.submenu')").addClass("block-el");
				
				$scrollDiv.css('height',$verticalSlideOutMenu.height() - 20);
				
				
			},
			
			addEvents : function()
			{
				$h3.click(function(){
					
					methods.headerSlide.apply(this);
				});
				
				$slide.click(function(){
					//get the xposition of the slider
					var cPos = $(this).position();
					if($(this).hasClass("slideOut"))
					{
						methods.slideOut.apply(this);
					}
					else
					{
						methods.slideIn.apply(this,[cPos.left]); //pass the position of the slider to slide In
					}
					
					
				});
				
				$(".submenu",self).click(function(){
					$(this).toggleClass("closed open");
					$(this).parent().next().slideToggle();
				});
				
				$("a.submenu,h3,.slide").hover(function(){
					$(this).addClass("hoverClass");
				},function(){
					$(this).removeClass("hoverClass");
				});
				
				$(".block-el",self).click(function(){
					methods.active.apply(this);
				});
			   
			   $(".block-el",self).hover(function(){
					$(this).addClass("text_hover");
			   },function(){
					$(this).removeClass("text_hover");
				
			   });
			   
			   
				if(options.slideInOnLoad)
				{
					$slide.trigger('click');
				}
			},
			slideIn : function(x)
			{
				var containerWidth = parseInt($verticalSlideOutMenu.outerWidth(true), 10) + 'px';
				$verticalSlideOutMenu.animate({left:'-'+containerWidth},'slow',$animationMethod,function(){
					$slide.toggleClass("slideIn slideOut");
				});
				$(this).animate({
					left:0
				},'slow',$animationMethod);
				$(this).data('posX',x); //add the original position of the slider as data of the object
					
			},
				 
				  
				 
			slideOut : function()
			{
				var psX = $(this).data('posX'); //get the original position X of the slider for the object data
				$verticalSlideOutMenu.animate({
					left:'-1px'},'slow',$animationMethod,function(){
						$slide.toggleClass("slideIn slideOut");
					});
				$(this).animate({
					left:psX //relocate to original position
				},'slow',$animationMethod);
					
			},
			headerSlide : function()
			{
				if(options.accordion)
				{
					var $child = $(this).children();		
					if($child.hasClass("expanded"))
					{
						return;
					}
					else
					{
						$("h3 > a.expanded",self).toggleClass("expanded collapsed").parent().next().slideToggle();
						$(this).children().toggleClass("expanded collapsed").parent().next().slideToggle();
					}
				}
				else
				{
					$(this).children().toggleClass("expanded collapsed");
					$(this).next().slideToggle();
				}	
			},
			active : function()
			{
				$("#active_sel").removeAttr("id");
				$(this).attr("id","active_sel");
				options.selected.apply($(this));
			},
			addSlideAnimation: function()
			{
				if($.isFunction( $.easing[options.slideAnimation]))
				{
					$animationMethod =  options.slideAnimation;
				}
				else
				{
					$animationMethod =  '';		
				}
			},
			addPositioning: function()
			{
				if (options.positionFixed === true) 
				{
					options.positioning = 'fixed';
				}
				else 
				{
					options.positioning = 'absolute';
				}
        
				//ie6 positioning
				if (document.all && !window.opera && !window.XMLHttpRequest) 
				{
					options.positioning = 'absolute';
				}
				
				$verticalSlideOutMenu.css('position',options.positioning);
				$slide.css('position',options.positioning);
			}
			
			
			
		};	
		 
		
		methods.init();
		methods.addEvents();
		methods.addSlideAnimation();
		methods.addPositioning();
		
	   
	   
}})(jQuery);
		